package com.bupt.greeddaotest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.bupt.greeddaotest.bean.User;
import com.bupt.greeddaotest.control.MeApplication;
import com.bupt.greeddaotest.dao.UserDao;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private UserDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String host = getApplicationContext().getDatabasePath(MeApplication.database_name).getAbsolutePath();
        dao = MeApplication.getMeApplication().getDaoSession().getUserDao();
        System.out.println("开始插入数据  " + System.currentTimeMillis());
        User u1 = new User(null, "jack", 10, "f", "111111111","22");
        dao.insert(u1);
        System.out.println("============" + System.currentTimeMillis());
        show();
//        for (int j = 0; j < 30; j++) {
//            List<User> userList = new ArrayList<>();
//            for (int i = 0; i < 1000; i++) {
//                User u;
//                if (i / 2 == 0) {
//                    u = new User(null, "jack" + i, 10 + i / 10, "f", "111111111");
//                } else {
//                    u = new User(null, "jack" + i, 10 + i / 10, "m", "111111111");
//                }
//                userList.add(u);
//            }
//            dao.insertInTx(userList);
//            System.out.println("======================"+j);
//        }
//        String s = new Gson().toJson(userList);
//        System.out.println(s);

//        System.out.println("结束插入数据  " + System.currentTimeMillis());
//        show();

//        for (int i = 0; i < 9000; i++) {
//            User u = new User(null, "jack" + i, 10 + i / 10, "m", "111111111");
//            userList.add(u);
//        }
//        dao.insertInTx(userList);

        System.out.println("开始查询数据  " + System.currentTimeMillis());
        List<User> users = dao.queryBuilder().orderAsc(UserDao.Properties.Id).limit(10000).build().list();

        System.out.println("查询结束" + System.currentTimeMillis());
        if (users.size() > 0) {
            System.out.println(users.size());
//            for (User user : users) {
//                System.out.println(user.toString());
//            }
//            dao.updateInTx(users);
        } else {
            System.out.println("not ");
        }
        show();
//        System.out.println("开始删除：" + System.currentTimeMillis());
//        users = dao.loadAll();
//        dao.deleteInTx(users);
//        System.out.println("==============================deleteAl===============" + System.currentTimeMillis());
//        show();
//        Log.d("=================", "onCreate: " + host);
//        File file = new File("/data/data/com.bupt.greeddaotest/databases");
//        System.out.println(file.length());
    }

    private void show() {
        List<User> users = dao.loadAll();
        System.out.println("===============================" + dao.count());
        for (User user : users
                ) {
            System.out.println(user.toString());
        }
    }
}
